import BaseService from './base';
import Sequelize from 'sequelize';
const Op = Sequelize.Op;

export default class LogosService extends BaseService {
  constructor({ logosDao }) {
    super();
    this.logosDao = logosDao
  }

  async getLogosAll (logos) {
    const [err, list] = await this.logosDao.getLogosAll({
      where: {
        mark: {[Op.ne]: logos.mark}
      }
    });
    if (err) {
      return this.fail('获取列表失败', null, err);
    }
    return this.success('查询成功', list);
  }

  async getLogosList(logos) {
    const [err, count, rows] = await this.logosDao.getLogosList({
      where: {
        AGENT_USERNAME: logos.AGENT_USERNAME === '' ? {
          [Op.ne]: null
        } : logos.AGENT_USERNAME,
        mark: {[Op.ne]: logos.mark}
      },
      limit: logos.limit,
      offset: logos.limit * (logos.offset - 1),
      distinct: true
    });
    if (err) {
      return this.fail('获取列表失败', null, err);
    }
    return this.success('查询成功', count, rows);
  }

  async addLogos(logos) {
    const [exsitErr, itemExsit] = await this.logosDao.findOne({
      where: {
        AGENT_USERNAME: logos.AGENT_USERNAME
      }
    });
    if (exsitErr) {
      return this.fail('添加失败', null, exsitErr);
    }
    if (itemExsit) {
      return this.fail('已存在');
    }
    const [addErr] = await this.logosDao.addLogos(logos);
    if (addErr) {
      return this.fail('添加失败', null, addErr);
    }
    return this.success('添加成功')
  }

  async delLogos(logos) {
    const [err, updateLogos] = await this.logosDao.update(logos, {
      where: {
        id: logos.id
      }
    });
    if (err) {
      return this.fail('删除失败');
    }
    return this.success('删除成功');
  }

  async updateLogos(logos) {
    const [err, updateLogos] = await this.logosDao.update(logos, {
      where: {
        id: logos.id
      }
    });
    if (err) {
      return this.fail('更新失败');
    }
    return this.success('更新成功');
  }
}